package com.zretc.device.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;

import com.zretc.device.dao.Device_InDao;
import com.zretc.device.dao.Device_NeedDao;
import com.zretc.device.dao.Device_OutDao;
import com.zretc.device.entity.Device_In;
import com.zretc.device.entity.Device_Need;
import com.zretc.device.entity.Device_Out;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPopupMenu;
import java.awt.Component;
import javax.swing.JMenuItem;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class Device_needFindDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTable table;
	private JTextField txtneed_id;
	private JTextField txtdepartment;
	private JTextField txtdevice_code;
	private JTextField txtneed_num;
	private JTextField txtbegin_date;
	private JTextField txtend_date;
	private JTextField txtneed_person;
	private JTextField txtInput;
	private JTextField txtneed_tele;
	private JPopupMenu popupMenu;
	private JMenuItem mntmNewMenuItem;
	private String need_id;
	private JLabel label_1;
	private JComboBox txtCB;
	private JButton button_2;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			Device_needFindDialog dialog = new Device_needFindDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public Device_needFindDialog() {
		setBounds(100, 100, 919, 555);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(14, 176, 807, 277);
		contentPanel.add(scrollPane);
		// 创建表
		table = new JTable();
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				// 取值
				need_id = table.getValueAt(table.rowAtPoint(e.getPoint()), 0) + "";
				String department = table.getValueAt(table.rowAtPoint(e.getPoint()), 1) + "";
				String device_code = table.getValueAt(table.rowAtPoint(e.getPoint()), 2) + "";
				String need_num = table.getValueAt(table.rowAtPoint(e.getPoint()), 3) + "";
				String begin_date = table.getValueAt(table.rowAtPoint(e.getPoint()), 4) + "";
				String end_date = table.getValueAt(table.rowAtPoint(e.getPoint()), 5) + "";
				String need_person = table.getValueAt(table.rowAtPoint(e.getPoint()), 6) + "";
				String need_tele = table.getValueAt(table.rowAtPoint(e.getPoint()), 7) + "";
				// 赋值
				txtneed_id.setText(need_id);
				txtdepartment.setText(department);
				txtdevice_code.setText(device_code);
				txtneed_num.setText(need_num);
				txtbegin_date.setText(begin_date);
				txtend_date.setText(end_date);
				txtneed_person.setText(need_person);
				txtneed_tele.setText(need_tele);
			}
		});
		table.setRowHeight(25);

		popupMenu = new JPopupMenu();
		addPopup(table, popupMenu);

		mntmNewMenuItem = new JMenuItem("删除");
		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 让用户选择
				int n = JOptionPane.showConfirmDialog(null, "请问是否继续删除？删除后将无法恢复！", "删除需求单号为: " + need_id + " 的所有信息",
						JOptionPane.YES_NO_OPTION);
				if (n == 0) {
					// 转型
					int needId = Integer.parseInt(need_id);
					// 获取编号
					Device_NeedDao dnd = new Device_NeedDao();
					boolean flag = dnd.delDevNeed(needId);
					if (flag) {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "删除成功");
					} else {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "删除失败", "消息", JOptionPane.ERROR_MESSAGE);
					}
					// 更新
					fillTable(scrollPane);
				}
			}
		});
		popupMenu.add(mntmNewMenuItem);
		fillTable(scrollPane);

		txtneed_id = new JTextField();
		txtneed_id.setEditable(false);
		txtneed_id.setColumns(10);
		txtneed_id.setBounds(14, 120, 86, 30);
		contentPanel.add(txtneed_id);

		txtdepartment = new JTextField();
		txtdepartment.setColumns(10);
		txtdepartment.setBounds(97, 120, 86, 30);
		contentPanel.add(txtdepartment);

		txtdevice_code = new JTextField();
		txtdevice_code.setColumns(10);
		txtdevice_code.setBounds(184, 120, 86, 30);
		contentPanel.add(txtdevice_code);

		txtneed_num = new JTextField();
		txtneed_num.setColumns(10);
		txtneed_num.setBounds(267, 120, 86, 30);
		contentPanel.add(txtneed_num);

		txtbegin_date = new JTextField();
		txtbegin_date.setColumns(10);
		txtbegin_date.setBounds(352, 120, 86, 30);
		contentPanel.add(txtbegin_date);

		txtend_date = new JTextField();
		txtend_date.setColumns(10);
		txtend_date.setBounds(437, 120, 86, 30);
		contentPanel.add(txtend_date);

		txtneed_person = new JTextField();
		txtneed_person.setColumns(10);
		txtneed_person.setBounds(520, 120, 86, 30);
		contentPanel.add(txtneed_person);

		JButton button = new JButton("确认修改");
		button.setFont(new Font("宋体", Font.PLAIN, 20));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 取值
				// 需求id
				String tneed_id = txtneed_id.getText();
				// 需求部门
				String department = txtdepartment.getText();
				// 设备编号
				String device_code = txtdevice_code.getText();
				// 需求数量
				String tneed_num = txtneed_num.getText();
				// 开始时间
				String begin_date = txtbegin_date.getText();
				// 结束时间
				String end_date = txtend_date.getText();
				// 申请人
				String need_person = txtneed_person.getText();
				// 申请人电话
				String tneed_tele = txtneed_tele.getText();

				// 判断输入的值
				if (tneed_id.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "单号不能为空");
				} else if (department.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "使用部门不能为空");
				} else if (device_code.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "设备编号不能为空");
				} else if (tneed_num.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "需求数量不能为空");
				} else if (begin_date.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "起始日期不能为空");
				} else if (end_date.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "截止日期不能为空");
				} else if (need_person.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "需求人不能为空");
				} else if (tneed_tele.equals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "需求人电话不能为空");
				} else {
					if (tneed_tele.matches("^((13[0-9])|(15[^4])|(17[^9])|(18[^14]))\\d{8}$")) {
						// 需求id
						int need_id = Integer.parseInt(tneed_id);
						// 需求数量
						int need_num = Integer.parseInt(tneed_num);
						// 创建对象
						Device_Need dn = new Device_Need(need_id, department, device_code, need_num, begin_date,
								end_date, need_person, tneed_tele);
						Device_NeedDao dnd = new Device_NeedDao();
						boolean flag = dnd.modDevNeed(dn);
						if (flag) {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "修改成功");
							fillTable(scrollPane);
						} else {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "修改失败");
						}
					}else {
						// 弹窗提示
						JOptionPane.showMessageDialog(null, "需求人电话不是一个正确的号码");
					}
				}
			}
		});
		button.setBounds(723, 120, 115, 27);
		contentPanel.add(button);

		JButton button_1 = new JButton("查询");
		button_1.setFont(new Font("宋体", Font.PLAIN, 20));
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
//				// 获取复选框里的值
				String opString = txtCB.getSelectedItem() + "";
				// 获取文本框里的值
				String txt = txtInput.getText();
				if (txt.contentEquals("")) {
					// 弹窗提示
					JOptionPane.showMessageDialog(null, "查询内容不能为空");
				} else {
					if (opString.equals("需求单号")) {
						// 调用方法
						int needId = Integer.parseInt(txt);
						Device_Need di = fillTableById(scrollPane, needId);
						if (di == null) {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "该单号不存在");
						}
					} else if (opString.equals("设备编号")) {
						// 调用方法
						List<Device_Need> list = fillTableByCode(scrollPane, txt);
						if (list.isEmpty()) {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "该设备编号不存在");
						}
					}

					else if (opString.equals("起始日期")) {
						// 调用方法
						List<Device_Need> list = fillTableByBegDate(scrollPane, txt);
						if (list.isEmpty()) {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "该起始日期不存在");
						}
					} else if (opString.equals("结束日期")) {
						// 调用方法
						List<Device_Need> list = fillTableByEndDate(scrollPane, txt);
						if (list.isEmpty()) {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "该结束日期不存在");
						}
					} else if (opString.equals("用户名")) {
						// 调用方法
						List<Device_Need> list = fillTableByUser(scrollPane, txt);
						if (list.isEmpty()) {
							// 弹窗提示
							JOptionPane.showMessageDialog(null, "该申请人不存在");
						}
					}
				}
			}
		});
		button_1.setBounds(575, 77, 113, 27);
		contentPanel.add(button_1);

		txtInput = new JTextField();
		txtInput.setFont(new Font("宋体", Font.PLAIN, 25));
		txtInput.setColumns(10);
		txtInput.setBounds(217, 77, 344, 30);
		contentPanel.add(txtInput);

		JLabel label = new JLabel("请输入查询信息");
		label.setFont(new Font("宋体", Font.PLAIN, 25));
		label.setBounds(30, 77, 186, 30);
		contentPanel.add(label);

		txtneed_tele = new JTextField();
		txtneed_tele.setColumns(10);
		txtneed_tele.setBounds(606, 120, 103, 30);
		contentPanel.add(txtneed_tele);

		label_1 = new JLabel("请选择查询方式");
		label_1.setFont(new Font("宋体", Font.PLAIN, 25));
		label_1.setBounds(30, 26, 181, 38);
		contentPanel.add(label_1);

		txtCB = new JComboBox();
		txtCB.setModel(new DefaultComboBoxModel(new String[] { "需求单号", "设备编号", "起始日期", "结束日期", "用户名" }));
		txtCB.setFont(new Font("宋体", Font.PLAIN, 20));
		txtCB.setBounds(236, 32, 130, 30);
		contentPanel.add(txtCB);

		button_2 = new JButton("取消");
		button_2.setFont(new Font("宋体", Font.PLAIN, 20));
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtInput.setText("");
				fillTable(scrollPane);
			}
		});
		button_2.setBounds(699, 77, 113, 27);
		contentPanel.add(button_2);
		// 居中
		setLocationRelativeTo(null);
	}

	/**
	 * 查询所有
	 * 
	 * @param scrollPane
	 */
	private void fillTable(JScrollPane scrollPane) {
		// 创建对象
		Device_NeedDao dnd = new Device_NeedDao();
		// 调用查找所有的方法
		List<Device_Need> list = dnd.findDevNeedAll();
		int row = list.size();
		Object[][] obj = new Object[row][8];
		// 循环遍历
		for (int i = 0; i < obj.length; i++) {
			Device_Need dn = list.get(i);
			// 赋值
			obj[i][0] = dn.getNeed_id();
			obj[i][1] = dn.getDepartment();
			obj[i][2] = dn.getDevice_code();
			obj[i][3] = dn.getNeed_num();
			obj[i][4] = dn.getBegin_date();
			obj[i][5] = dn.getEnd_date();
			obj[i][6] = dn.getNeed_person();
			obj[i][7] = dn.getNeed_tele();
		}
		table.setModel(new DefaultTableModel(obj,
				new String[] { "\u9700\u6C42\u5355\u53F7", "\u4F7F\u7528\u90E8\u95E8", "\u8BBE\u5907\u7F16\u53F7",
						"\u9700\u6C42\u6570\u91CF", "\u8D77\u59CB\u65E5\u671F", "\u7ED3\u675F\u65E5\u671F",
						"\u7533\u8BF7\u4EBA", "\u7533\u8BF7\u4EBA\u7535\u8BDD" }));
		scrollPane.setViewportView(table);
	}

	/**
	 * 根据单号查询
	 * 
	 * @param scrollPane
	 */
	private Device_Need fillTableById(JScrollPane scrollPane, int needId) {
		// 创建对象
		Device_NeedDao dnd = new Device_NeedDao();
		Device_Need dn = dnd.findDevNeedById(needId);
		if (dn != null) {
			// 创建二维数组
			Object[][] obj = new Object[1][8];
			// 赋值
			obj[0][0] = dn.getNeed_id();
			obj[0][1] = dn.getDepartment();
			obj[0][2] = dn.getDevice_code();
			obj[0][3] = dn.getNeed_num();
			obj[0][4] = dn.getBegin_date();
			obj[0][5] = dn.getEnd_date();
			obj[0][6] = dn.getNeed_person();
			obj[0][7] = dn.getNeed_tele();

			table.setModel(new DefaultTableModel(obj,
					new String[] { "\u9700\u6C42\u5355\u53F7", "\u4F7F\u7528\u90E8\u95E8", "\u8BBE\u5907\u7F16\u53F7",
							"\u9700\u6C42\u6570\u91CF", "\u8D77\u6B62\u65E5\u671F", "\u7ED3\u675F\u65E5\u671F",
							"\u9700\u6C42\u4EBA", "\u9700\u6C42\u4EBA\u7535\u8BDD" }));
			scrollPane.setViewportView(table);
		}
		return dn;

	}

	/**
	 * 根据用户名
	 * 
	 * @param scrollPane
	 */
	private List<Device_Need> fillTableByUser(JScrollPane scrollPane, String userName) {
		// 创建对象
		Device_NeedDao dnd = new Device_NeedDao();
		// 调用查找所有的方法
		List<Device_Need> list = dnd.findDevNeedByUser(userName);
		if (list != null) {
			int row = list.size();
			Object[][] obj = new Object[row][8];
			// 循环遍历
			for (int i = 0; i < obj.length; i++) {
				Device_Need dn = list.get(i);
				// 赋值
				obj[i][0] = dn.getNeed_id();
				obj[i][1] = dn.getDepartment();
				obj[i][2] = dn.getDevice_code();
				obj[i][3] = dn.getNeed_num();
				obj[i][4] = dn.getBegin_date();
				obj[i][5] = dn.getEnd_date();
				obj[i][6] = dn.getNeed_person();
				obj[i][7] = dn.getNeed_tele();
			}
			table.setModel(new DefaultTableModel(obj,
					new String[] { "\u9700\u6C42\u5355\u53F7", "\u4F7F\u7528\u90E8\u95E8", "\u8BBE\u5907\u7F16\u53F7",
							"\u9700\u6C42\u6570\u91CF", "\u8D77\u6B62\u65E5\u671F", "\u7ED3\u675F\u65E5\u671F",
							"\u9700\u6C42\u4EBA", "\u9700\u6C42\u4EBA\u7535\u8BDD" }));
			scrollPane.setViewportView(table);
		}

		return list;
	}

	/**
	 * 根据设备
	 * 
	 * @param scrollPane
	 */
	private List<Device_Need> fillTableByCode(JScrollPane scrollPane, String devCode) {
		// 创建对象
		Device_NeedDao dnd = new Device_NeedDao();
		// 调用查找所有的方法
		List<Device_Need> list = dnd.findDevNeedByCode(devCode);
		if (list != null) {
			int row = list.size();
			Object[][] obj = new Object[row][8];
			// 循环遍历
			for (int i = 0; i < obj.length; i++) {
				Device_Need dn = list.get(i);
				// 赋值
				obj[i][0] = dn.getNeed_id();
				obj[i][1] = dn.getDepartment();
				obj[i][2] = dn.getDevice_code();
				obj[i][3] = dn.getNeed_num();
				obj[i][4] = dn.getBegin_date();
				obj[i][5] = dn.getEnd_date();
				obj[i][6] = dn.getNeed_person();
				obj[i][7] = dn.getNeed_tele();
			}
			table.setModel(new DefaultTableModel(obj,
					new String[] { "\u9700\u6C42\u5355\u53F7", "\u4F7F\u7528\u90E8\u95E8", "\u8BBE\u5907\u7F16\u53F7",
							"\u9700\u6C42\u6570\u91CF", "\u8D77\u6B62\u65E5\u671F", "\u7ED3\u675F\u65E5\u671F",
							"\u9700\u6C42\u4EBA", "\u9700\u6C42\u4EBA\u7535\u8BDD" }));
			scrollPane.setViewportView(table);
		}

		return list;
	}

	/**
	 * 根据起始日期
	 * 
	 * @param scrollPane
	 */
	private List<Device_Need> fillTableByBegDate(JScrollPane scrollPane, String begDate) {
		// 创建对象
		Device_NeedDao dnd = new Device_NeedDao();
		// 调用查找所有的方法
		List<Device_Need> list = dnd.findDevNeedByBegDate(begDate);
		if (list != null) {
			int row = list.size();
			Object[][] obj = new Object[row][8];
			// 循环遍历
			for (int i = 0; i < obj.length; i++) {
				Device_Need dn = list.get(i);
				// 赋值
				obj[i][0] = dn.getNeed_id();
				obj[i][1] = dn.getDepartment();
				obj[i][2] = dn.getDevice_code();
				obj[i][3] = dn.getNeed_num();
				obj[i][4] = dn.getBegin_date();
				obj[i][5] = dn.getEnd_date();
				obj[i][6] = dn.getNeed_person();
				obj[i][7] = dn.getNeed_tele();
			}
			table.setModel(new DefaultTableModel(obj,
					new String[] { "\u9700\u6C42\u5355\u53F7", "\u4F7F\u7528\u90E8\u95E8", "\u8BBE\u5907\u7F16\u53F7",
							"\u9700\u6C42\u6570\u91CF", "\u8D77\u6B62\u65E5\u671F", "\u7ED3\u675F\u65E5\u671F",
							"\u9700\u6C42\u4EBA", "\u9700\u6C42\u4EBA\u7535\u8BDD" }));
			scrollPane.setViewportView(table);
		}

		return list;
	}

	/**
	 * 根据结束日期
	 * 
	 * @param scrollPane
	 */
	private List<Device_Need> fillTableByEndDate(JScrollPane scrollPane, String endDate) {
		// 创建对象
		Device_NeedDao dnd = new Device_NeedDao();
		// 调用查找所有的方法
		List<Device_Need> list = dnd.findDevNeedByEndDate(endDate);
		if (list != null) {
			int row = list.size();
			Object[][] obj = new Object[row][8];
			// 循环遍历
			for (int i = 0; i < obj.length; i++) {
				Device_Need dn = list.get(i);
				// 赋值
				obj[i][0] = dn.getNeed_id();
				obj[i][1] = dn.getDepartment();
				obj[i][2] = dn.getDevice_code();
				obj[i][3] = dn.getNeed_num();
				obj[i][4] = dn.getBegin_date();
				obj[i][5] = dn.getEnd_date();
				obj[i][6] = dn.getNeed_person();
				obj[i][7] = dn.getNeed_tele();
			}
			table.setModel(new DefaultTableModel(obj,
					new String[] { "\u9700\u6C42\u5355\u53F7", "\u4F7F\u7528\u90E8\u95E8", "\u8BBE\u5907\u7F16\u53F7",
							"\u9700\u6C42\u6570\u91CF", "\u8D77\u6B62\u65E5\u671F", "\u7ED3\u675F\u65E5\u671F",
							"\u9700\u6C42\u4EBA", "\u9700\u6C42\u4EBA\u7535\u8BDD" }));
			scrollPane.setViewportView(table);
		}

		return list;
	}

	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}

			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}

			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
}
